home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / mj90.lha / ProPage / StopwatchTimerIX-37.ppd (.txt) < prev    next >
Encoding:
Professional Page Document  |  1991-06-12  |  24.2 KB  |  216 lines

  1. Professional Page V1.2
  2. dPENV
  3. Helvetica
  4. Bookman
  5. Courier
  6. zapfdingbats
  7. helvetican
  8. >Green
  9. ~Blue
  10. Yellow
  11. Magenta
  12. >Cyan
  13. '(PDOC
  14.  timerIX37.ppg
  15. TAdam Levin
  16. Disabling Sprite DMA from a Vertical Blank Interrupt
  17. HuPBOX
  18. LdPTXT
  19. Z\n\B\ff<Courier>\fs<7>\jf\lr<105>\ls<0.000>\ps<100>\t<0>\c<Black>U259 U260 U261 U262\.
  20. h#PBOX
  21. j,PBOX
  22. W\n\ff<helvetican>\fs<10>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Socketed Chip RAM\.
  23. KPBOX
  24. X\n\ff<helvetican>\fs<10>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Soldered Chip\MRAM\.
  25. Z\n\B\ff<Courier>\fs<7>\jf\lr<105>\ls<0.000>\ps<100>\t<0>\c<Black>U263 U264 U265 U266\.
  26. Z\n\B\ff<Courier>\fs<7>\jf\lr<105>\ls<0.000>\ps<100>\t<0>\c<Black>U271 U272 U273 U274\.
  27. y\n\B\ff<Courier>\fs<7>\jf\lr<105>\ls<0.000>\ps<100>\t<0>\c<Black>                               U267 U268 U269 U270\.
  28.  PPAG
  29. jPSGR
  30. Black
  31. Black
  32. ePBOX
  33. (PBOX
  34. l\n\B\ff<Helvetica>\fs<10>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Timers, Serial Port and Parallel Port\.
  35. -_PBOX
  36. V\n\B\ff<Helvetica>\fs<10>\jc\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Page IX - 38\.
  37. KQPBOX
  38. s\n\B\ff<Helvetica>\fs<10>\jr\lr<125>\ls<0.000>\ps<100>\t<0>\c<Black>Stopwatch Functions With the timer.device\.
  39. xbPBOX
  40. `\n\B\I\O\ff<Helvetica>\fs<30>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Amiga Mail\ff<Times>
  41. /PBOX
  42. Black
  43. Black
  44. oPBOX
  45. OPBOX
  46. Black
  47. Black
  48. p\n\B\ff<Helvetica>\fs<10>\lr<125>\ls<0.000>\ps<100>\t<0>\c<Black>Stopwatch Functions With the timer.device\.
  49. V\n\B\ff<Helvetica>\fs<10>\jc\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Page IX - 39\.
  50. 1PBOX
  51. o\n\B\ff<Helvetica>\fs<10>\jr\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Timers, Serial Port and Parallel Port\.
  52. <PTXT
  53. V\n\B\I\O\ff<Helvetica>\fs<30>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Amiga Mail
  54. *    PBOX
  55. Black
  56. Black
  57. .rPPAG
  58. .`PBOX
  59. .WPTXT
  60. \n\B\ff<Times>\fs<30>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>
  61. Stopwatch Functions with the timer.device\fs<14>
  62. by Mike Sinz
  63. \fs<12>\lr<125>To measure time on the Amiga the system software provides the \Itimer.device.\i  The \Itimer.device\i is very handy for many purposes, however, it does not have a built-in stopwatch.  It does have some time manipulation functions that make this a simple thing to do though.  This article shows how to do stopwatch functions using the \Itimer.device.\i
  64. The example code contains four routines needed to set up and use the stopwatch.
  65. The routines are:
  66. \ff<zapfdingbats>\fs<10>o\ff<Times>\s\ff<Courier>struct timerequest  *Init_Timer  (VOID)\ff<Times>
  67. \ff<zapfdingbats>o\ff<Times>\s\ff<Courier>VOID \sTimer_Start (struct timerequest *)\ff<Times>
  68. \ff<zapfdingbats>o\ff<Times>\s\ff<Courier>VOID \sTimer_Stop  (struct timerequest *)\ff<Times>
  69. \ff<zapfdingbats>o\ff<Times>\s\ff<Courier>VOID \sFree_Timer  (struct timerequest *)\ff<Times>\fs<12>
  70. The stopwatch functions all use a struct timerequest as their main parameter.  This is just the usual Amiga IORequest structure with two extra fields for seconds and microseconds.  Init_Timer() sets up the \Itimer.device\i and returns a pointer to a struct timerequest.  This pointer is used in Timer_Start() and Timer_Stop() which measure the elapsed time.  Finally, Free_Timer() is used to close the \Itimer.device\i and free the memory used for the timerequest.
  71. An example main() function is listed below to show how to use the stopwatch functions to  measure elapsed time.  The example just does a Delay(73L) and then displays the amount of time that took.  Some uses for the program might include measuring how long a student takes to answer a question, the amount of time a player has been playing, or how fast the system is.  For more information on the \Itimer.device,\i refer to the\I ROM Kernel Manual: Libraries and Devices\i (p. 871, ISBN 0-201-18187-8).\.
  72. kPBOX
  73. LPSGR
  74. Black
  75. Black
  76. gPBOX
  77. p\n\B\ff<Helvetica>\fs<10>\lr<125>\ls<0.000>\ps<100>\t<0>\c<Black>Stopwatch Functions with the timer.device\.
  78. V\n\B\ff<Helvetica>\fs<10>\jc\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Page IX - 37\.
  79. o\n\B\ff<Helvetica>\fs<10>\jr\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Timers, Serial Port and Parallel Port\.
  80. WnPBOX
  81. `\n\B\I\O\ff<Helvetica>\fs<30>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Amiga Mail\ff<Times>
  82. Black
  83. Black
  84. P\n\I\ff<Times>\fs<12>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>May/June 1990\.
  85. [PSGR
  86. Black
  87. Black
  88. 7PBOX
  89. \n\ff<Courier>\fs<9>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>/*
  90.  \s* Example stopwatch functions using the Amiga timer.device...
  91.  \s*/
  92.  \s* Makefile used to compile with Lattice C 5.04 or 5.05
  93. # MakeFile for StopWatch
  94. CFLAGS= -b1 -cfirstq -ms0 -rr1 -v -w
  95. OBJS=   StopWatch.o
  96. LIBS=   LIB:lcsr.lib
  97. .c.o:
  98.     @LC $(CFLAGS) $*
  99. StopWatch: $(OBJS)
  100.     @BLink FROM LIB:c.o $(OBJS) TO StopWatch LIB $(LIBS) SMALLDATA SMALLCODE
  101. #include    <exec/types.h>
  102. #include    <exec/memory.h>
  103. #include    <devices/timer.h>
  104. #include    <proto/exec.h>
  105. #include    <proto/timer.h>
  106. #include    <proto/dos.h>
  107. #include    <stdio.h>
  108. \s* The library base for the timer...
  109. struct  Library *TimerBase=NULL;
  110. \s* This gets the starting time...
  111. VOID Timer_Start(struct timerequest *Time_Req)
  112. \s   Time_Req->tr_node.io_Command=TR_GETSYSTIME;
  113. \s   Time_Req->tr_node.io_Flags=IOF_QUICK;
  114. \s   DoIO((struct IORequest *)Time_Req);
  115. \s* This gets the ending time and computes the time difference
  116. \s* in the timerequest->tr_time.
  117. VOID Timer_Stop(struct timerequest *Time_Req)
  118. struct  timeval StartTime;
  119. \s   StartTime=Time_Req->tr_time;
  120. \s   Time_Req->tr_node.io_Command=TR_GETSYSTIME;
  121. \s   Time_Req->tr_node.io_Flags=IOF_QUICK;
  122. \s   DoIO((struct IORequest *)Time_Req);
  123. \s   SubTime(&(Time_Req->tr_time),&StartTime);
  124. \s* Initialize the stopwatch...
  125. struct timerequest *Init_Timer(VOID)
  126. register    struct  timerequest *Time_Req=NULL;
  127. register    struct  MsgPort     *port=NULL;
  128. \s   if (port=CreatePort(NULL,NULL))
  129. \s   {
  130. \s       if (Time_Req=(struct timerequest *)CreateExtIO(port,
  131. \s                                               sizeof(struct timerequest)))
  132. \s       {
  133. \s           if (!OpenDevice(TIMERNAME,UNIT_VBLANK,
  134. \s                           (struct IORequest *)Time_Req,NULL))
  135. \s           {
  136. \s               TimerBase=(struct Library *)Time_Req->tr_node.io_Device;
  137. \s           }
  138. \s           else
  139. \s           {
  140. \s               DeleteExtIO((struct IORequest *)Time_Req);
  141. \s               Time_Req=NULL;
  142. \s           }
  143. \s       }
  144. \s       if (!Time_Req)
  145. \s       {
  146. \s           DeletePort(port);
  147. \s           port=NULL;
  148. \s       }
  149. \s   }
  150. \s   return(Time_Req);
  151. \s* Free up the timer...
  152. VOID Free_Timer(struct timerequest *Time_Req)
  153. \s   if (Time_Req)
  154. \s   {
  155. \s       CloseDevice((struct IORequest *)Time_Req);
  156. \s       DeletePort(Time_Req->tr_node.io_Message.mn_ReplyPort);
  157. \s       DeleteExtIO((struct IORequest *)Time_Req);
  158. \s   }
  159. \s* A simple main() to use these features...
  160. VOID main(int argc, char *argv[])
  161. register    struct  timerequest *StopWatch;
  162. \s   if (argc)   /* Check if CLI... */
  163. \s   {
  164. \s       if (StopWatch=Init_Timer())
  165. \s       {
  166. \s           Timer_Start(StopWatch);
  167. \s           /* Now we do something... */
  168. \s           Delay(73L);
  169. \s           Timer_Stop(StopWatch);
  170. \s           /* Now we display the results... */
  171. \s           printf("Time taken: %ld.%06ld seconds\\n",
  172. \s                       StopWatch->tr_time.tv_secs,
  173. \s                       StopWatch->tr_time.tv_micro);
  174. \s           /* Free the structure again... */
  175. \s           Free_Timer(StopWatch);
  176. \s       }
  177. \s   }
  178. \B\I\O\ff<Bookman>\fs<14>A\.
  179.     ]wPTXT
  180. l\n\B\ff<Helvetica>\fs<10>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Timers, Serial Port and Parallel Port\.
  181. WPBOX
  182. =PTXT
  183. V\n\B\ff<Helvetica>\fs<10>\jc\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Page IX - 38\.
  184. s\n\B\ff<Helvetica>\fs<10>\jr\lr<125>\ls<0.000>\ps<100>\t<0>\c<Black>Stopwatch Functions with the timer.device\.
  185. "PTXT
  186. `\n\B\I\O\ff<Helvetica>\fs<30>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Amiga Mail\ff<Times>
  187. GPBOX
  188. ,PSGR
  189. Black
  190. Black
  191. uPSGR
  192. Black
  193. Black
  194. p\n\B\ff<Helvetica>\fs<10>\lr<125>\ls<0.000>\ps<100>\t<0>\c<Black>Stopwatch Functions with the timer.device\.
  195. V\n\B\ff<Helvetica>\fs<10>\jc\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Page IX - 39\.
  196. o\n\B\ff<Helvetica>\fs<10>\jr\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Timers, Serial Port and Parallel Port\.
  197. l^PTXT
  198. V\n\B\I\O\ff<Helvetica>\fs<30>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Amiga Mail
  199. NPSGR
  200. Black
  201. Black
  202. ]PBOX
  203. Black
  204. Black
  205. sPBOX
  206. qPBOX
  207. l\n\B\ff<Helvetica>\fs<10>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Timers, Serial Port and Parallel Port\.
  208. V\n\B\ff<Helvetica>\fs<10>\jc\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Page IX - 40\.
  209. s\n\B\ff<Helvetica>\fs<10>\jr\lr<125>\ls<0.000>\ps<100>\t<0>\c<Black>Stopwatch Functions with the timer.device\.
  210. lPBOX
  211. JPTXT
  212. `\n\B\I\O\ff<Helvetica>\fs<30>\lr<120>\ls<0.000>\ps<100>\t<0>\c<Black>Amiga Mail\ff<Times>
  213. %vPSGR
  214. Black
  215. Black
  216.